package com.rocpenghua.codec;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author pengp
 * @date 2022年04月15日 12:43
 */
public class TimeTool {
    private static final String FILE_PATH = "E:\\excel\\";
    private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
    public interface Task{
        void execute() throws Exception;
    }
    public static void check(String title,Task task) throws Exception {
        if(task == null){
            return;
        }
        title = (title == null) ? "":("【"+title+"】");
        System.out.println(title);
//        System.out.println("开始：" + sdf.format(new Date()));
        long begin = System.currentTimeMillis();
        task.execute();
        long end = System.currentTimeMillis();
//        System.out.println("结束："+sdf.format(new Date()));
        long delta = (end - begin);

        //存入文件 [^a-zA-Z]

        System.out.println("耗时-----"+delta+"----毫秒");
        addContentSecond(FILE_PATH+title.replaceAll("[^0-9a-zA-Z]","")+".txt" ,title);
        addContentSecond(FILE_PATH+title.replaceAll("[^0-9a-zA-Z]","")+".txt" ,"耗时-----"+delta+"----毫秒");
        System.out.println("-----------------------");
    }

    public static void addContentSecond(String filePath, String newContent) {
        FileWriter fileWriter = null;
        try {
            fileWriter=new FileWriter(filePath,true);
            fileWriter.write(newContent+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
